<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use App\Models\SmsLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function getProfile()
    {
        return response()->json(['code' => 1, 'data' => Auth::user()->load(['pointLogs', 'pointBagLogs', 'coupons', 'orders', 'apply'])]);
    }

    public function getPointLogs()
    {
        $pointLogs = Auth::user()->pointLogs;
        return response()->json(['code' => 1, 'data' => $pointLogs]);
    }

    public function getPointBagLogs()
    {
        $pointBagLogs = Auth::user()->pointBagLogs;
        return response()->json(['code' => 1, 'data' => $pointBagLogs]);
    }

    public function getCoupons()
    {
        $coupons = Auth::user()->coupons;
        return response()->json(['code' => 1, 'data' => $coupons]);
    }

    public function getOrders()
    {
        $orders = Auth::user()->orders;
        return response()->json(['code' => 1, 'data' => $orders]);
    }

    public function bindPhoneNumberBySms(Request $request)
    {
        $user = Auth::user();
        $sms = SmsLog::query()->where('phone_number', $request->input('phone_number'))->where('type', 'bind')->latest()->first();
        if ($user->phone_number) {
            return response()->json(['code' => 1001, 'message' => '已绑定手机号'], 400);
        }
        if ($request->input('code') == $sms->code) {
            $user->phone_number = $request->input('phone_number');
            $user->save();
            return response()->json(['code' => 1, 'message' => '手机号绑定成功']);
        } else {
            return response()->json(['code' => 1002, 'message' => '验证码不正确'], 400);
        }
    }


}
